{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这是一个标准,`@dataclass` 是 Python 3.7 引入的标准库装饰器，它是为了简化类的定义，特别是那些用于存储数据的类。\n",
    "\n",
    "不仅如此,这个标准还提供了类型提示.遗憾的是这个装饰器和typing本身没有多大的关系."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# bad case\n",
    "class MyClass:\n",
    "\n",
    "    def func1(self):\n",
    "        try:\n",
    "            # \"\"\"\"code for func1\"\"\n",
    "            logger.info(\"func1 success\")\n",
    "        except Exception as e:\n",
    "            logger.error(str(e))\n",
    "\n",
    "    def func2(self):\n",
    "        try:\n",
    "            # \"\"\"\"code for func2\"\"\n",
    "            logger.info(\"func2 success\")\n",
    "        except Exception as e:\n",
    "            logger.error(str(e))\n",
    "\n",
    "    def func3(self):\n",
    "        try:\n",
    "            # \"\"\"\"code for func3\"\"\n",
    "            logger.info(\"func3 success\")\n",
    "        except Exception as e:\n",
    "            logger.error(str(e))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_exception_and_logging(func):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        try:\n",
    "            res = func(*args, **kwargs)\n",
    "            logger.info(f\"{func.__name__} success\")\n",
    "            return res\n",
    "        except Exception as e:\n",
    "            logger.error(str(e))\n",
    "    return wrapper\n",
    "\n",
    "# better\n",
    "class Myclass:\n",
    "    @handle_exception_and_logging\n",
    "    def func1(self):\n",
    "        pass  # 假设这里有一些代码\n",
    "\n",
    "    @handle_exception_and_logging\n",
    "    def func2(self):\n",
    "        pass  # 假设这里有一些代码\n",
    "\n",
    "    @handle_exception_and_logging\n",
    "    def func3(self):\n",
    "        pass  # 假设这里有一些代码"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
